package ModelLayer;

import java.util.ArrayList;
/**
 * Write a description of class LoanContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanContainer
{
    private ArrayList<Loan> loanCollection;
    private DVDContainer dvdCont;

    //Loan singleton
    private static LoanContainer instance;

    public LoanContainer()
    {
        loanCollection = new ArrayList<Loan>();
        dvdCont = new DVDContainer();
    }
    /**
     * returns instance(singleton)
     * @return instance
       */
    public static LoanContainer getInstance()
    {
        if(instance == null)
        {
            instance = new LoanContainer();
        }
        return instance;
    }
    /**
     * add loan
     * @param Loan newLoan
       */
    public void addLoan(Loan newLoan)
    {
        loanCollection.add(newLoan);
    }
    /**
     * returns loan
     * @param Long id
     * @return Loan thisLoan
       */
    public Loan getLoan(long id)
    {
        boolean found = false;
        int index = 0;
        Loan thisLoan = null;
        while(!found && index < loanCollection.size())
        {
            thisLoan = loanCollection.get(index); // gets the container
            if(thisLoan.getId() == id)
            {
                found = true;
            }
            else
            {
                index++;
            }
        }

        if(found)
        {
            return thisLoan;
        }
        else
        {
            return null;
        }
    }
    /**
     * deletes a loan
     * @param long id
     * 
       */
    public void deleteLoan(long id)
    {
        Loan l = getLoan(id);
        loanCollection.remove(l);
    }
    /**
     * lists all loans
     * 
       */
    public void listAllLoans()
    {
        for(Loan l: loanCollection){
            System.out.println("Id: " + l.getId() + "\t" + "Borrow Date: " + l.getBorrowDate() + "\t" + "Period: " + l.getPeriod() + "\t" + "State: " + l.getState());
        }
    }

}
